package com.easy.controller;

import com.easy.bean.Doctor;
import com.easy.service.DoctorService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("doctor")
public class DoctorContorller {

    @Autowired
    DoctorService doctorService;
    // 新增分页接口（放在 {id} 接口之前）
    // 查询的分页接口
    @GetMapping("page") // 路径为 /doctor/page
    public Result page(Page page, Doctor doctor) { // 接收分页参数和查询条件
        Object obj = doctorService.page(page, doctor); // 调用服务层分页逻辑
        return Result.success(obj);
    }
    //增
    @PostMapping
    public Result add(@RequestBody Doctor doctor){
        Object obj = doctorService.add(doctor);
        return Result.success(obj);
    }

    //改
    @PutMapping
    public Result edit(@RequestBody Doctor doctor){
        Object obj = doctorService.edit(doctor);
        return Result.success(obj);
    }

    //删
    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj = doctorService.del(id);
        return Result.success(obj);
    }

    //查
    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj = doctorService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Doctor doctor){
        Object obj = doctorService.all(doctor);
        return Result.success(obj);
    }

    //分页查询医生信息并关联部门信息
    @GetMapping("/pageWithDepartment")
    public Map<String, Object> pageWithDepartment(
            Doctor doctor,
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "limit",   defaultValue = "10") int limit) {
        return doctorService.pageWithDepartment(doctor, current, limit);
    }
}
